package com.company.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.eduservice.entity.EduChapter;
import com.company.eduservice.entity.EduVideo;
import com.company.eduservice.mapper.EduChapterMapper;
import com.company.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.eduservice.service.EduVideoService;
import com.company.eduservice.vo.chapter.ChapterVo;
import com.company.eduservice.vo.chapter.VideoVo;
import com.company.servicebase.exception.MyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-18
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    private EduVideoService eduVideoService;

    //根据课程ID查询所有查找大纲列表
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        List<EduChapter> chapters = this.baseMapper.selectList(new QueryWrapper<EduChapter>().eq("course_id", courseId));

        List<ChapterVo> collect = chapters.stream().map((entity) -> {
            List<EduVideo> videos = eduVideoService.list(new QueryWrapper<EduVideo>().eq("chapter_id", entity.getId()));

            List<VideoVo> videoVos = videos.stream().map((video) -> {
                VideoVo videoVo = new VideoVo();
                videoVo.setId(video.getId());
                videoVo.setTitle(video.getTitle());
                videoVo.setVideoSourceId(video.getVideoSourceId());
                return videoVo;
            }).collect(Collectors.toList());

            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(entity.getId());
            chapterVo.setTitle(entity.getTitle());
            chapterVo.setChildren(videoVos);
            return chapterVo;
        }).collect(Collectors.toList());

        return collect;
    }

    //添加课程大纲的章节
    @Override
    @Transactional
    public void addChapterByCourse(EduChapter eduChapter) {
        this.baseMapper.insert(eduChapter);
    }

    //根据章节ID查询章节
    @Override
    public EduChapter findChapterById(String chapterId) {
        EduChapter eduChapter = this.getById(chapterId);
        return eduChapter;
    }

    //修改课程大纲的章节
    @Override
    @Transactional
    public void updateChapter(EduChapter eduChapter) {
        this.updateById(eduChapter);
    }

    //删除章节
    @Override
    public void deleteChapterById(String chapterId) {
        int count = eduVideoService.count(new QueryWrapper<EduVideo>().eq("chapter_id", chapterId));
        if (0 < count) {
            throw new MyException(10002, "该章节有视频，不能删除");
        } else {
            this.baseMapper.deleteById(chapterId);
        }
    }

    @Override
    public void deleteChapterByCourseId(String courseId) {
        this.baseMapper.delete(new QueryWrapper<EduChapter>().eq("course_id", courseId));
    }
}
